package Stack;

/**
 * @author yyq
 * @create 2021-10-08 16:14
 */
public class ArrayStack_cc<T> {
    private int Max_Size;
    //top始终指向数据上方
    private int top;
    private Object[] stack;
    private static ArrayStack<Character> s2=new ArrayStack<Character>(100);

    public ArrayStack_cc(int max_Size) {
        top=0;
        Max_Size = max_Size;
        stack=new Object[max_Size];

    }
    public void realPush(T val){
        stack[top]=val;
        top++;
    }


    public void push(T val,int end){

        if(!isEmpty()){
            //检查是否超过Maxsize
            char value=val.toString().charAt(0);
            //1 如果是数字直接入栈
            if(Character.isDigit(value)){
                stack[top]=val;
                top++;
                if(end==-1){
                    realPush((T)s2.pop());
                }
                return;
            }
            //2 如果是括号
            if(value=='('||value==')'){
                //2.1如果是左括号直接入栈s2
                if(value=='('){
                    s2.push(value);
                    return;
                }
                //2.2如果是右括号 ），在遇到左括号 （ 之前把栈S2所有的运算符放到主栈S1中
                if(value==')'){
                    Character temp;
                    while ((temp=s2.pop())!='('){
                        realPush((T) temp);
                    }
                    return;
                }
            }
            //3 如果是运算符
            if(value=='+'||value=='-'||value=='*'||value=='/'){
                //3.1 如果栈空直接放入
                if(s2.isEmpty()){
                    s2.push(value);
                    return;
                }
               //获取优先级
                int val_Priority = getValPriority(value);
                Character temp=s2.pop();
                int s2_Top_Priority = getValPriority(temp);

                //3.2 非空 如果大于等于栈顶的优先级 直接放入S2栈
                if(val_Priority>s2_Top_Priority){
                    s2.push(temp);
                    s2.push(value);
                    return;
                }else
                    //维持入栈时栈顶的优先级小于等于入栈的优先级
                    {
                        while(true){
                            if(val_Priority>s2_Top_Priority){
                                s2.push(temp);
                                s2.push(value);
                                return;
                            }
                            realPush((T) temp);
                            if(s2.isEmpty()){
                                s2.push(value);
                                return;
                            }
                            temp=s2.pop();
                            s2_Top_Priority=getValPriority(temp);
                        }
                }
            }

        }

        if(val.toString().charAt(0)=='('){
            s2.push(val.toString().charAt(0));
        }
        if(Character.isDigit(val.toString().charAt(0))){
            realPush(val);
        }

    }

    public int getValPriority(Character value){
        int pri;
        if(value=='+') return pri=1;
        if(value=='-') return pri=1;
        if(value=='*') return pri=2;
        if(value=='/') return pri=2;
        return 0;
    }

    public Boolean isEmpty(){
        if(top==0) return true;
        else return false;
    }

    public  T pop() {
        if(top==0){
            System.out.println("没得数据了，老表");
            return null;
        }
        if(top!=0){
            top--;
            return (T) stack[top];
        }
        return null;
    }

    public int getTop(){
        return top;
    }

}
